<?php 
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id

	+ Module  		: m_pasal Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_pasal.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 11:40:14

*/

class M_m_pasal extends CI_Model{

		//constructor
		function __construct() {
			parent::__construct();
		}

		//function for get list record
		function m_pasal_list($filter,$start,$end){
			$sql = "SELECT pasal_id,
						   pasal_seq,
						   pasal_type,
						   pasal_no,
						   pasal_title,
						   pasal_content,
						   pasal_active,
						   pasal_created_date,
						   pasal_created_by,
						   pasal_updated_date,
						   pasal_updated_by,
						   pasal_updated_count					FROM   m_pasal";

			// For simple search
			if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (
						   lower(pasal_type) LIKE '%".addslashes(strtolower($filter))."%' OR
						   lower(pasal_seq) LIKE '%".addslashes(strtolower($filter))."%' OR
						   lower(pasal_title) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." ORDER BY pasal_type, pasal_seq  LIMIT ".$start.", ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function m_pasal_create($pasal_seq ,$pasal_no ,$pasal_title ,$pasal_content ,$pasal_active,$pasal_type ,$pasal_created_by ){
			$kuery_checkseq = "SELECT pasal_id FROM m_pasal WHERE pasal_type = '". $pasal_type ."' AND pasal_seq = '". $pasal_seq ."'";
			$query_check_seq = $this->db->query($kuery_checkseq);
			if($query_check_seq->num_rows() > 0){
				return 4;
			}
			
			$kuery_checkno = "SELECT pasal_id FROM m_pasal WHERE pasal_type = '". $pasal_type ."' AND pasal_no = '". $pasal_no ."'";
			$query_check_no = $this->db->query($kuery_checkno);
			if($query_check_no->num_rows() > 0){
				return 3;
			}
			
			$data = array(
					"pasal_seq"=>$pasal_seq,
					"pasal_no"=>$pasal_no,
					"pasal_title"=>$pasal_title,
					"pasal_content"=>$pasal_content,
					"pasal_active"=>$pasal_active,
					"pasal_type"=>$pasal_type,
					"pasal_created_by"=>$pasal_created_by					
					);
			$this->db->insert('m_pasal', $data);
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}

		//function for update record
		function m_pasal_update($pasal_id,$pasal_seq,$pasal_no,$pasal_title,$pasal_content,$pasal_active,$pasal_type,$pasal_updated_date,$pasal_updated_by){
			$data = array(
					"pasal_seq"=>$pasal_seq,
					"pasal_no"=>$pasal_no,
					"pasal_title"=>$pasal_title,
					"pasal_content"=>$pasal_content,
					"pasal_active"=>$pasal_active,
					"pasal_type"=>$pasal_type,
					"pasal_updated_date"=>$pasal_updated_date,
					"pasal_updated_by"=>$pasal_updated_by			
					);

			$this->db->where('pasal_id', $pasal_id);
			$this->db->update('m_pasal', $data);
			if($this->db->affected_rows()){
				$sqlu = "UPDATE m_pasal SET pasal_updated_count = (pasal_updated_count + 1) WHERE pasal_id = ".$pasal_id;
				$this->db->query($sqlu);
				return '1';
			}else {
				return '0';
			}
			
		}

		//fcuntion for delete record
		function m_pasal_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the m_pasals at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM m_pasal WHERE pasal_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM m_pasal WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "pasal_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function m_pasal_search($pasal_id,
				 $pasal_seq,
				 $pasal_no,
				 $pasal_title,
				 $pasal_content,
				 $pasal_active,
				 $pasal_created_date,
				 $pasal_created_by,
				 $pasal_updated_date,
				 $pasal_updated_by,
				 $pasal_updated_count,$start,$end){
			//full query
			$sql = "SELECT pasal_id,
						   pasal_seq,
						   pasal_no,
						   pasal_title,
						   pasal_content,
						   pasal_type,
						   pasal_active,
						   pasal_created_date,
						   pasal_created_by,
						   pasal_updated_date,
						   pasal_updated_by,
						   pasal_updated_count
					FROM   m_pasal";

			/* if($pasal_id!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_id) LIKE '%".addslashes(strtolower($pasal_id))."%'";
			}; */
			if($pasal_seq!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_seq) LIKE '%".addslashes(strtolower($pasal_seq))."%'";
			};
			if($pasal_no!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_no) LIKE '%".addslashes(strtolower($pasal_no))."%'";
			};
			if($pasal_title!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_title) LIKE '%".addslashes(strtolower($pasal_title))."%'";
			};
			if($pasal_content!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_content) LIKE '%".addslashes(strtolower($pasal_content))."%'";
			};
			if($pasal_active!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_active) LIKE '%".addslashes(strtolower($pasal_active))."%'";
			};
			/* if($pasal_created_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_created_date) LIKE '%".addslashes(strtolower($pasal_created_date))."%'";
			};
			if($pasal_created_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_created_by) LIKE '%".addslashes(strtolower($pasal_created_by))."%'";
			};
			if($pasal_updated_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_updated_date) LIKE '%".addslashes(strtolower($pasal_updated_date))."%'";
			};
			if($pasal_updated_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_updated_by) LIKE '%".addslashes(strtolower($pasal_updated_by))."%'";
			};
			if($pasal_updated_count!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(pasal_updated_count) LIKE '%".addslashes(strtolower($pasal_updated_count))."%'";
			}; */

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function m_pasal_print($pasal_id,
				 $pasal_seq,
				 $pasal_no,
				 $pasal_title,
				 $pasal_content,
				 $pasal_active,
				 $pasal_created_date,
				 $pasal_created_by,
				 $pasal_updated_date,
				 $pasal_updated_by,
				 $pasal_updated_count,$option,$filter){
			//full query
			$sql = "SELECT pasal_id,
						   pasal_seq,
						   pasal_no,
						   pasal_title,
						   pasal_content,
						   pasal_active,
						   pasal_created_date,
						   pasal_created_by,
						   pasal_updated_date,
						   pasal_updated_by,
						   pasal_updated_count
				    FROM   m_pasal";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(pasal_seq) LIKE '%".addslashes(strtolower($filter))."%' OR lower(pasal_title) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				/* if($pasal_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_id) LIKE '%".addslashes(strtolower($pasal_id))."%'";
				}; */
				if($pasal_seq!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_seq) LIKE '%".addslashes(strtolower($pasal_seq))."%'";
				};
				if($pasal_no!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_no) LIKE '%".addslashes(strtolower($pasal_no))."%'";
				};
				if($pasal_title!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_title) LIKE '%".addslashes(strtolower($pasal_title))."%'";
				};
				if($pasal_content!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_content) LIKE '%".addslashes(strtolower($pasal_content))."%'";
				};
				if($pasal_active!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_active) LIKE '%".addslashes(strtolower($pasal_active))."%'";
				};
				/* if($pasal_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_created_date) LIKE '%".addslashes(strtolower($pasal_created_date))."%'";
				};
				if($pasal_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_created_by) LIKE '%".addslashes(strtolower($pasal_created_by))."%'";
				};
				if($pasal_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_updated_date) LIKE '%".addslashes(strtolower($pasal_updated_date))."%'";
				};
				if($pasal_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_updated_by) LIKE '%".addslashes(strtolower($pasal_updated_by))."%'";
				};
				if($pasal_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_updated_count) LIKE '%".addslashes(strtolower($pasal_updated_count))."%'";
				}; */
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function m_pasal_export_excel($pasal_id ,$pasal_seq ,$pasal_no ,$pasal_title ,$pasal_content ,$pasal_active ,$pasal_created_date ,$pasal_created_by ,$pasal_updated_date ,$pasal_updated_by ,$pasal_updated_count ,$option,$filter){
			//full query
			$sql = "SELECT pasal_id,
						   pasal_seq,
						   pasal_no,
						   pasal_title,
						   pasal_content,
						   pasal_active,
						   pasal_created_date,
						   pasal_created_by,
						   pasal_updated_date,
						   pasal_updated_by,
						   pasal_updated_count				    FROM   m_pasal";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(pasal_seq) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(pasal_title) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				/* if($pasal_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_id) LIKE '%".addslashes(strtolower($pasal_id))."%'";
				}; */
				if($pasal_seq!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_seq) LIKE '%".addslashes(strtolower($pasal_seq))."%'";
				};
				if($pasal_no!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_no) LIKE '%".addslashes(strtolower($pasal_no))."%'";
				};
				if($pasal_title!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_title) LIKE '%".addslashes(strtolower($pasal_title))."%'";
				};
				if($pasal_content!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_content) LIKE '%".addslashes(strtolower($pasal_content))."%'";
				};
				if($pasal_active!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_active) LIKE '%".addslashes(strtolower($pasal_active))."%'";
				};
				/* if($pasal_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_created_date) LIKE '%".addslashes(strtolower($pasal_created_date))."%'";
				};
				if($pasal_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_created_by) LIKE '%".addslashes(strtolower($pasal_created_by))."%'";
				};
				if($pasal_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_updated_date) LIKE '%".addslashes(strtolower($pasal_updated_date))."%'";
				};
				if($pasal_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_updated_by) LIKE '%".addslashes(strtolower($pasal_updated_by))."%'";
				};
				if($pasal_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(pasal_updated_count) LIKE '%".addslashes(strtolower($pasal_updated_count))."%'";
				}; */
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>